﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.Sql;
using System.Data.SqlClient;

public class CustomUserDetailes
{
    public string id;
    public string UserName;
}

public partial class UserProfile : System.Web.UI.Page
{
    public string getUserId(string Nume){
        DataContext db = new DataContext("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True;");
        Table<aspnet_User> aspnet_User = db.GetTable<aspnet_User>();
        IQueryable<CustomUserDetailes> userQuery = from p in aspnet_User
                                                   where p.UserName == Nume
                                                   select new CustomUserDetailes { id = p.UserId };
        //"Select * from profile where id = '" + id + "'";
        foreach (CustomUserDetailes i in userQuery)
        {
            return i.id;
        }
        return null;
    }
    public string getUserName(string vid)
    {
        DataContext db = new DataContext("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True;");
        Table<aspnet_User> aspnet_User = db.GetTable<aspnet_User>();
        IQueryable<CustomUserDetailes> userQuery = from p in aspnet_User
                                                   where p.UserId == vid
                                                   select new CustomUserDetailes { UserName = p.UserName };
        //"Select * from profile where id = '" + id + "'";
        foreach (CustomUserDetailes i in userQuery)
        {
            return i.UserName;
        }
        return null;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        String id = System.Convert.ToString(Request["id"]);
        LabelNume.Text ="Profilul lui "+ getUserName(id);
        SqlDataPrieteni.SelectCommand=
            "select distinct usr.UserName AS UserName, pr.status AS Status, usr.UserId AS id from Prieteni as pi " +
            "join Profile as pr on (pr.id = pi.IdPrieten) " +
            "join aspnet_Users as usr on (usr.UserId=pi.IdPrieten) " +
            "where (IdUser = '" + id + "') "+
            "Union "+
            "select distinct usr.UserName AS UserName, pr.status AS Status, usr.UserId AS id from Prieteni as pi " +
            "join Profile as pr on (pr.id = pi.IdUser) " +
            "join aspnet_Users as usr on (usr.UserId=pi.IdUser) " +
            "where (IdPrieten = '" + id + "')";
        if (id == "") id = "0";
        SqlDataProfile.SelectCommand = "Select * from profile where id = '" + id + "'";
        DataContext db = new DataContext("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True;");
        Table<aspnet_User> aspnet_User = db.GetTable<aspnet_User>();
        IQueryable<CustomUserDetailes> userQuery = from p in aspnet_User
                                                         where p.UserName == Page.User.Identity.Name
                                                      select new CustomUserDetailes { id = p.UserId ,UserName=p.UserName};
        //"Select * from profile where id = '" + id + "'";
        foreach (CustomUserDetailes i in userQuery)
        {
            if (Page.User.Identity.Name!="") if (Page.User.Identity.Name == getUserName(id))
            {
                LoginViewFriendButtons.FindControl("IdRemoveFriend").Visible = false;
                LoginViewFriendButtons.FindControl("IdAddFriend").Visible = false;
            }
            if(i.id==id)ButtonEdit.Visible=true;
        }
    }
    protected void ButtonEdit_Click(object sender, EventArgs e)
    {
       Server.Transfer("~/UserProfileEdit.aspx?id=" + System.Convert.ToString(Request["id"]));
      
    }
    protected void Add_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.mdf;Integrated Security=True;User Instance=True");
            string ins = "INSERT INTO Prieteni (IdUser,IdPrieten) VALUES (@IdUser,@IdPrieten)";
            DataContext db = new DataContext("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True;");
            con.Open();
            SqlCommand a = new SqlCommand(ins, con);
            a.Parameters.AddWithValue("IdPrieten", System.Convert.ToString(Request["id"]));
            Table<aspnet_User> user = db.GetTable<aspnet_User>();
            IQueryable<CustomUserDetailes> userquery = from h in user
                                                  where h.UserName == Page.User.Identity.Name
                                                       select new CustomUserDetailes { id = h.UserId };
            foreach (CustomUserDetailes i in userquery)
            {
                a.Parameters.AddWithValue("IdUser", i.id);
            }
            a.ExecuteNonQuery();
            con.Close();
            Server.Transfer("~/Confirm.aspx?msg=" + "Adaugarea ca prieten in lista ");
        }
        catch (Exception er)
        {
            string t = "EROARE " + er.Message;
        }
    }
    protected void Delete_Click(object sender, EventArgs e)
    {
        try
        {
            string idUserLogat = getUserId(Convert.ToString(Page.User.Identity.Name));
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.mdf;Integrated Security=True;User Instance=True");
            string query = "DELETE FROM Prieteni WHERE (IdPrieten = '" + System.Convert.ToString(Request["id"]) + "') AND (IdUser = '" + idUserLogat + "')";
            con.Open();
            SqlCommand a = new SqlCommand(query, con);
            a.ExecuteNonQuery();
            con.Close();
            Server.Transfer("~/Confirm.aspx?msg=" + "Stergerea prietenului din lista ");
        }
        catch (Exception er)
        {
            string t = "EROARE " + er.Message;
        }
    }
}
